package com.huige.wiki.controller;

import com.huige.wiki.req.CategoryQueryReq;
import com.huige.wiki.req.CategorySaveReq;
import com.huige.wiki.resp.CategoryQueryResp;
import com.huige.wiki.resp.PageResp;
import com.huige.wiki.service.CategoryService;
import com.huige.wiki.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "分类相关接口")
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("/list")
    @ApiOperation("查询分类")
    public Result<PageResp<CategoryQueryResp>> list(@Validated CategoryQueryReq categoryQueryReq){
        log.info("查询分类列表参数，{}",categoryQueryReq);
        PageResp<CategoryQueryResp> pageResp = categoryService.list(categoryQueryReq);
        return Result.success(pageResp);
    }

    @GetMapping("/all")
    @ApiOperation("查询所有分类数据")
    public Result<List<CategoryQueryResp>> all(){
        log.info("没有查询所有分类列表参数");
        List<CategoryQueryResp> list = categoryService.all();
        return Result.success(list);
    }

    @PostMapping("/save")
    @ApiOperation("编辑分类（新增或修改）")
    public Result save(@RequestBody @Validated CategorySaveReq categoryQueryReq){
        categoryService.save(categoryQueryReq);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除分类")
    public Result delete(@PathVariable Long id){
        categoryService.delete(id);
        return Result.success();
    }


}
